﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineCooperation.DB;

namespace OnlineCooperation.Entity
{
    public class Post
    {
        public int PostID;                           //新鲜事的编号
        public int StateID;                         //若为新鲜事，则此属性取0；若为回复，则按照回复先后顺序编号（正整数）
        public String Content;
        public int TopicID;                        //如果post中有##包含的关键字，则创建话题；post至多只属于一个话题。
        public int TeamID;
        public DateTime DateAndTime;

        //空构造函数
        public Post()
        {
            PostID = 0;
            StateID = 0;
            Content = null;
            TopicID = -1;
            TeamID = 0;
            DateAndTime = new DateTime();
        }

        //给定除TopicID外其余参数的构造函数
        public Post(int postid, int stateid, String content, int teamid, DateTime dateandtime)
        {
            PostID = postid;
            StateID = stateid;
            Content = content;
            int first = content.IndexOf('#');
            TeamID = teamid;
            DateAndTime = dateandtime;
            if (first == -1)
                TopicID = -1;                                                                                    //该post不含任何话题
            else
            {
                int second = content.IndexOf('#', first + 1);
                if (second == -1)
                    TopicID = -1;                                                                               //该post不含任何话题
                else
                {
                    String TopicContent = content.Substring(first + 1, second - first - 1);
                    TopicDB TDB = new TopicDB();
                    TopicID = TDB.GetTopicByContent(TopicContent, teamid).ID;
                    if (TopicID == -1)
                    {
                        //该post利用##创建话题
                        Topic NewTopic = new Topic(-1, TopicContent, teamid);
                        TDB.Insert(NewTopic);
                        TopicID = NewTopic.ID;
                    }
                }
            }
        }

        //给定所有参数的构造函数
        public Post(int postid, int stateid, String content, int topicid, int teamid, DateTime dateandtime)
        {
            PostID = postid;
            StateID = stateid;
            Content = content;
            TopicID = topicid;
            TeamID = teamid;
            DateAndTime = dateandtime;
        }
    }
}